/**
 * 静态资源
 */
export function getStaticUrl(dir){
	const basis = getAudioResourcesUrl(dir)
	return basis
}
/**
 * 获取音频资源地址
 */
export function getAudioResourcesUrl(dir){
	const basis = 'https://file.web-jshtml.cn/times'
	return `${basis}${dir || ''}`
}
/**
 * 生成资源路径
 */
export function getResourcesUrl(dir){
	const basis = 'https://file.web-jshtml.cn/times/pingyi'
	return `${basis}${dir || ''}`
}

/** 获取远程地址资源 
 * @param {Object} url
 */
export function downfileUrl(url){
	return new Promise((resolve, reject) => {
		uni.downloadFile({
		    url, // 请求地址
		    responseType:'arraybuffer',
		    header:{},
		    success:(data)=>{// 请求成功的回调
				resolve({data, url})
		    },
		    fail:(err)=>{  //下载失败的回调
		      reject(false)
		    }
		})
	})
}

export function urlImageToBase64(url){
	return new Promise((resolve, reject) => {
		//图片路径转换为Base64↓
		let image = new Image(); // 一定要设置为let，不然图片不显示
		image.setAttribute('crossOrigin', '*'); // 解决跨域问题
		image.src = url + '?time=' + new Date().valueOf();//网络图片路径
		image.onload = () => {
			var canvas = document.createElement("canvas");
			canvas.width = image.width;
			canvas.height = image.height;
			var context = canvas.getContext("2d");
			context.fillStyle = "#fff";   
			context.fillRect(0, 0, canvas.width, canvas.height);  
			context.drawImage(image, 0, 0, image.width, image.height);
			var quality = 0.8;
			var dataURL = canvas.toDataURL("image/jpeg", quality); 
			//成功
			resolve(dataURL); 
		};
		
		image.onerror = () => {
			alert('失败')
			reject()
		}
	})
}